import apiBinderFactory from '@/utils/apiBinderFactory';
import filterValidKeyFactory from '@/utils/filterValidKeyFactory';

const prefix = 'task';

async function create({ content, remindTime }) {
  return await this.$axios.$post(`/${prefix}`, {
    content,
    remindTime
  });
}

async function getList(params={}) {
  return await this.$axios.$get(`/${prefix}/list`, { params });
}

async function getById(id) {
  return await this.$axios.$get(`/${prefix}/${id}`);
}

const filterValidKeyOfUpdatedTask = filterValidKeyFactory(['content', 'remindTime', 'isCompleted']);

async function updateById(id, data) {
  return await this.$axios.$put(`/${prefix}/${id}`, filterValidKeyOfUpdatedTask(data));
}

async function deleteById(id) {
  return await this.$axios.$delete(`/${prefix}/${id}`);
}

export default apiBinderFactory({
  create,
  getList,
  getById,
  updateById,
  deleteById
});
